﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace NCRGEA.Admin
{
    public partial class frmManageAccounts : Form
    {
        SqlConnection sqlConn;
        SqlDataAdapter sqlAdapter;
        public frmManageAccounts()
        {
            InitializeComponent();
        }

        private void frmManageAccounts_Load(object sender, EventArgs e)
        {
            RefreshData();
        }

        public void RefreshData()
        {
            if (sqlConn == null || sqlConn.State != ConnectionState.Open)
                sqlConn = AAL.DAL.GetSqlConnection();
            if (sqlConn == null || sqlConn.State != ConnectionState.Open)
                return;

            string qry = "SELECT StaffID,Username,Password,FirstName,LastName,Active FROM usrAccounts";
            sqlAdapter = new SqlDataAdapter(qry, sqlConn);

            SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(sqlAdapter);
            DataTable tbl = new DataTable();
            sqlAdapter.Fill(tbl);
            grdAccounts.DataSource = tbl;            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (sqlConn == null || sqlConn.State != ConnectionState.Open)
                sqlConn = AAL.DAL.GetSqlConnection();
            if (sqlConn == null || sqlConn.State != ConnectionState.Open)
            {
                MessageBox.Show("Error: Failed to save accounts!", "NCRGEA");
                return;
            }

            try
            {
                sqlAdapter.Update((DataTable)grdAccounts.DataSource);
            }
            catch (Exception eX)
            {
                MessageBox.Show("Couldn't update accounts! Did you forget to fill in a field?", "NCRGEA");
                Logger.Log("Couldn't save/update member accounts: " + eX.Message);
            }
        }

        private void frmManageAccounts_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (sqlConn != null)
            {
                if (sqlConn.State != ConnectionState.Closed)
                {
                    sqlConn.Close();
                }
                sqlConn.Dispose();
            }
        }

        private void grdAccounts_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show("Invalid value!");
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow r in grdAccounts.SelectedRows)
            {
                string uName = r.Cells["Username"].Value.ToString();
                DialogResult dr = MessageBox.Show("Are you sure that you want to delete user '" + uName + "'?", "NCRGEA - Manage Users", MessageBoxButtons.YesNoCancel);

                switch (dr)
                {
                    case DialogResult.Yes:
                        grdAccounts.Rows.RemoveAt(r.Index);
                        break;
                    case DialogResult.No:
                        break;
                    case DialogResult.Cancel:
                        return;
                }
            }
        }
    }
}
